package cn.com.huzo.webapp.controller.manufacture;

import cn.com.huzo.model.entities.manufacture.CommissionProcessingContractHeaderEntity;
import cn.com.huzo.model.entities.manufacture.CommissionProcessingContractLinesEntity;
import cn.com.huzo.model.entities.manufacture.CommissionProcessingContractLinesMoEntity;
import cn.com.huzo.service.manufacture.CommissionProcessingContractHeaderManager;
import cn.com.huzo.service.manufacture.CommissionProcessingContractLinesManager;
import cn.com.huzo.service.manufacture.CommissionProcessingContractLinesMoManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 加工承揽合同画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/manufacture/cpc")
public class CommissionProcessingContractController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "CommissionProcessingContractHeaderManager")
    private CommissionProcessingContractHeaderManager _manager_header;

    @Resource(name = "CommissionProcessingContractLinesManager")
    private CommissionProcessingContractLinesManager _manager_lines;

    @Resource(name = "CommissionProcessingContractLinesMoManager")
    private CommissionProcessingContractLinesMoManager _manager_lines_mo;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到加工承揽合同列表画面
     * <p>
     * @param request
     * @return
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("manufacture.commission_processing_contract.list.def");
        return view;
    }

    /**
     * 跳转到加工承揽合同编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param contractId 加工合同标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{contractId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "contractId") Long contractId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("contractId", contractId);
        view.setViewName("manufacture.commission_processing_contract.edit.def");
        return view;
    }

    /**
     * 跳转到加工承揽合同选择画面
     * <p>
     * @return
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.manufacture.commission_processing_contract.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("BillNo", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据ID取得加工承揽合同数据。
     *
     * @param criterion 加工承揽合同ID、当前用户、流程标识
     * @return 加工承揽合同数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByHeaderKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"));
    }

    /**
     * 根据条件取得加工承揽合同信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 加工承揽合同信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存加工承揽合同信息数据。
     * <p>
     * @param instance 要保存的加工承揽合同信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody CommissionProcessingContractHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {                         // 新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除加工承揽合同信息。
     *
     * @param criterion 要删除的数据
     * @return 删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    
    /**
     * 根据加工承揽合同标识取得款号明细
     *
     * @param headerKey 加工承揽合同标识
     * @return 款明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchStyleNoByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CommissionProcessingContractLinesMoEntity> fetchStyleNoByHeaderKey(@RequestBody Long headerKey) throws Exception {
        return _manager_header.fetchStyleNoByHeaderKey(headerKey);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="审核处理部分">
    /**
     * 审核加工承揽合同数据。
     * <p>
     * @param criterion 要审核加工承揽合同对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/check.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> check(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.doCheck(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "WorkFlowId"),
                MapUtils.getShort(criterion, "NodeOrder"),
                MapUtils.getLong(criterion, "CheckedBy"),
                MapUtils.getShort(criterion, "IsCheckCurrentNode")
        );
    }

    /**
     * 反审加工承揽合同数据。
     * <p>
     * @param criterion 要审核加工承揽合同对象。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "/uncheck.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> unCheck(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.doUnCheck(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "WorkFlowId"),
                MapUtils.getShort(criterion, "NodeOrder"),
                MapUtils.getLong(criterion, "CheckedBy")
        );
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据所选择的指示书,保存加工明细，同时更新合同明细、主表总金额。
     * <p>
     * @param itemList 要保存的明细。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "/mo_lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> inertIntoMoLines(@RequestBody List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        }
        return _manager_lines_mo.batchInsert(itemList);
    }

    /**
     * 更新加工承揽-合同明细信息数据。
     * <p>
     * @param itemList 要保存的分配明细。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "/mo_lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateMoLines(@RequestBody List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        }

        return _manager_lines_mo.batchUpdate(itemList);
    }

    /**
     * 删除加工承揽合同-加工明细信息。
     *
     * @param instance 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/mo_lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteMoLines(@RequestBody CommissionProcessingContractLinesMoEntity instance) throws Exception {
        return _manager_lines_mo.deleteLines(instance);
    }

    /**
     * 批量删除加工承揽合同-加工明细信息。
     *
     * @param itemList 要删除的数据
     * @return 原辅料出库明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/mo_lines/batchdelete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteMoLines(@RequestBody List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        }

        return _manager_lines_mo.batchDelete(itemList);
    }

    /**
     * 更新加工承揽-合同明细信息数据。
     * <p>
     * @param itemList 要保存的分配明细。
     * @return 影响的行数。
     * @throws Exception
     */
    @RequestMapping(value = "/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateContentLines(@RequestBody List<CommissionProcessingContractLinesEntity> itemList) throws Exception {
        if (itemList.isEmpty()) {
            return new HashMap();
        }

        return _manager_lines.batchUpdate(itemList);
    }
    /**
     * 获取加工合同明细信息，用于加工费计算。
     *
     * @param itemList 要提取明细数据的headerid列表
     * @return 加工合同明细。
     * @throws java.lang.Exception
     */
    /*
    @RequestMapping(value = "/lines/fetchLinesForCmtStatistics.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CommissionProcessingContractLinesEntity> fetchLinesForCmtStatistics(@RequestBody List<CommissionProcessingContractLinesEntity> itemList) throws Exception {
        return _linesContManager.fetchLinesForCmtStatistics(itemList);
    }*/
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="导出excel">
    /*
    @RequestMapping(value = "manufacture/export_to_excel.html",
            method = RequestMethod.POST)
    public ModelAndView exportCommissionProcessingContract(HttpServletRequest request) throws Exception {
        Long headerId = Long.parseLong(request.getParameter("_HeaderKey").trim());
        Map<String, Object> model = _manager_header.fetchByHeaderKey(headerId);

        return new ModelAndView("CommissionProcessingContractExcelView", model);
    }*/
    // </editor-fold>
}
